package com.ibest.base.listener;

import com.alibaba.fastjson.JSON;
import com.ibest.base.entity.CanalBinlogBean;
import com.ibest.base.expand.HandleInterceptEntrance;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.*;
import org.redisson.api.listener.MessageListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.Ordered;

import javax.annotation.Resource;

@Slf4j
public class RedisTopicListener implements ApplicationRunner, Ordered {
    @Resource
    private RedissonClient redissonClient;
    @Value("${ibest.client.topic-name}")
    private String topicName;
    @Resource
    private HandleInterceptEntrance handleInterceptEntrance;




    @Override
    public void run(ApplicationArguments args) throws Exception {
        RTopic rTopic=redissonClient.getTopic(topicName);
        rTopic.addListener(CanalBinlogBean.class,new MessageListener<CanalBinlogBean>(){
            @Override
            public void onMessage(CharSequence charSequence, CanalBinlogBean canalBinlogBean) {
                log.info("订阅：{},数据信息：{}",topicName, JSON.toJSONString(canalBinlogBean));
                handleInterceptEntrance.handleEntrance(canalBinlogBean);
            }
        });
    }

    @Override
    public int getOrder() {
        return 1;
    }
}
